package service

import (
	"context"
	"github.com/spf13/cast"
	"insighttracking/common/utils"
	"insighttracking/query/dao"
	"insighttracking/query/model"
)

type _appVersionRecordService struct {
}

func (s _appVersionRecordService) CreateApi(ctx context.Context, m *model.AppVersionRecordEntity) error {
	if err := dao.AppVersionRecordEntity.WithContext(ctx).Create(m); err != nil {
		return err
	}
	return nil
}

func (s _appVersionRecordService) GetPageApi(ctx context.Context, types string, version string, pageNumber int64, pageSize int64) (result []*model.AppVersionRecordEntity, count int64, err error) {
	tx := dao.AppVersionRecordEntity.WithContext(ctx)
	if types != "" {
		tx = tx.Where(dao.AppVersionRecordEntity.Types.Eq(cast.ToInt64(types)))
	}
	if version != "" {
		tx = tx.Where(dao.AppVersionRecordEntity.Version.Eq(version))
	}
	offset, limit := utils.DbPage(pageNumber, pageSize)
	return tx.Order(dao.AppVersionRecordEntity.ID.Desc()).FindByPage(offset, limit)
}

var AppVersionRecordService = new(_appVersionRecordService)
